package com.ruoyi.points.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.points.mapper.PointsRecordMapper;
import com.ruoyi.points.domain.PointsRecord;
import com.ruoyi.points.service.IPointsRecordService;
import com.ruoyi.points.service.ISysUserProfileService;
import com.ruoyi.points.domain.SysUserProfile;
import java.util.Date;

/**
 * 积分记录Service业务层处理
 * 
 * @author jion
 * @date 2025-08-13
 */
@Service
public class PointsRecordServiceImpl implements IPointsRecordService 
{
    @Autowired
    private PointsRecordMapper pointsRecordMapper;

    @Autowired
    private ISysUserProfileService sysUserProfileService;

    //一对一查询
    @Override
    public List<PointsRecord> selectPointsRecordAndUser(PointsRecord pointsRecord) {
        return pointsRecordMapper.pointsRecordAndUser(pointsRecord);
    }
    /**
     * 查询积分记录
     * 
     * @param id 积分记录主键
     * @return 积分记录
     */
    @Override
    public PointsRecord selectPointsRecordById(Long id)
    {
        return pointsRecordMapper.selectPointsRecordById(id);
    }

    /**
     * 查询积分记录列表
     * 
     * @param pointsRecord 积分记录
     * @return 积分记录
     */
    @Override
    public List<PointsRecord> selectPointsRecordList(PointsRecord pointsRecord)
    {
        return pointsRecordMapper.selectPointsRecordList(pointsRecord);
    }

    /**
     * 新增积分记录
     * 
     * @param pointsRecord 积分记录
     * @return 结果
     */
    @Override
    public int insertPointsRecord(PointsRecord pointsRecord)
    {
        pointsRecord.setCreateTime(DateUtils.getNowDate());
        return pointsRecordMapper.insertPointsRecord(pointsRecord);
    }

    /**
     * 修改积分记录
     * 
     * @param pointsRecord 积分记录
     * @return 结果
     */
    @Override
    public int updatePointsRecord(PointsRecord pointsRecord)
    {
        return pointsRecordMapper.updatePointsRecord(pointsRecord);
    }

    /**
     * 批量删除积分记录
     * 
     * @param ids 需要删除的积分记录主键
     * @return 结果
     */
    @Override
    public int deletePointsRecordByIds(Long[] ids)
    {
        return pointsRecordMapper.deletePointsRecordByIds(ids);
    }

    /**
     * 删除积分记录信息
     * 
     * @param id 积分记录主键
     * @return 结果
     */
    @Override
    public int deletePointsRecordById(Long id)
    {
        return pointsRecordMapper.deletePointsRecordById(id);
    }

    /**
     * ssh修改
     * 用户积分变动（增加或减少），并同步更新SysUserProfile积分
     */
    @Override
    @Transactional // ssh修改
    public int changeUserPoints(PointsRecord pointsRecord) {
        Long userId = pointsRecord.getUserId();
        Integer amount = pointsRecord.getAmount();
        Integer type = pointsRecord.getType();
        String title = pointsRecord.getTitle();
        String description = pointsRecord.getDescription();
        // 1. 查询当前总积分
        SysUserProfile profile = sysUserProfileService.selectByUserId(userId); // ssh修改
        if (profile == null) {
            throw new RuntimeException("用户档案不存在"); // ssh修改
        }
        int currentPoints = profile.getPoints() == null ? 0 : profile.getPoints();
        int newPoints;
        int typeInt = type.intValue();
        if (typeInt == 1) {
            newPoints = currentPoints + amount;
        } else if (typeInt == 0) {
            newPoints = currentPoints - amount;
            if (newPoints < 0) {
                throw new RuntimeException("积分不足");
            }
        } else {
            throw new RuntimeException("type参数错误，只能为0或1");
        }
        // 2. 新增积分记录
        PointsRecord record = new PointsRecord();
        record.setUserId(userId);
        record.setType(typeInt); // ssh修改
        record.setAmount(amount); // 始终为正数
        record.setTitle(title);
        record.setDescription(description);
        record.setDel(1);
        record.setCreateBy(null); // 可根据BaseEntity实际情况调整
        record.setCreateTime(new Date());
        pointsRecordMapper.insertPointsRecord(record);
        // 3. 更新用户档案积分
        sysUserProfileService.updatePointsByUserId(userId, newPoints); // ssh修改
        return 1;
    }
}
